extends ColorRect

class_name PingRect

# Declare member variables here. Examples:
# var a = 2
# var b = "text"

var ring_index = 2

onready var TOP_PANEL = find_parent("top_panel")
onready var SHIP = find_parent("SHIP")

var ping
var ping_label
var planet
var active = false

var ping_per_dist = 1 #ms

# Called when the node enters the scene tree for the first time.
func _ready():
	color = ColorN("black")
	
	ping_label = FontedLabel.new()
	ping_label.rect_size = rect_size
	ping_label.font_size = rect_size.y * 0.3
	ping_label.align = Label.ALIGN_CENTER
	ping_label.valign = Label.ALIGN_CENTER
	ping_label.text = "ping label"
	call_deferred("add_child",ping_label)

func activate():
	planet = TOP_PANEL.planet
	active = true
func deactivate():
	active = false

func _process(delta):
	if active:
		var ring
		var dist
		for r in range(len(planet.rings)):
			dist = (SHIP.global_position - planet.global_position).length()
			if dist <= planet.rings[r].radius:
				ring_index = r
				ring = planet.rings[r]
				break
		if !ring:
			TOP_PANEL.jam_progress_bar.reward = 0
			planet.jam_missed()
			return
		if SHIP.gameover:
			planet.finish_jam()
			return
		color = ring.default_color
		ping = dist - planet.radius 
		ping *= ping_per_dist
		
		ping_label.text = "PING: " + str(floor(ping)) + " ms"
		if color.r + color.g > 1.2:
			ping_label.modulate = ColorN("brown")
		else:
			ping_label.modulate = ColorN("white")
		print(ring_index)
